package pl.diagnoser.server.persistance.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import pl.diagnoser.client.dto.SearchResultAttributesDTO;
import pl.diagnoser.client.dto.SearchResultDTO;
import pl.diagnoser.client.tools.SearchObjectAttribute;
import pl.diagnoser.server.persistance.map.Objects;
import pl.diagnoser.server.persistance.map.SearchResult;
import pl.diagnoser.server.persistance.map.SearchResultAttributes;
import pl.diagnoser.server.persistance.tools.HibernateUtil;

public class SearchResultDao implements Serializable {

	private static final long serialVersionUID = 1L;
	
	public String saveSearching(List<SearchObjectAttribute> objectAttributes, long objectId) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		
		String info = "OK";
		String ok = "";
		Objects objects = new ObjectsDao().getObject(objectId);
		SearchResult searchResult = new SearchResult(objects);
		Long generatedId = (Long) session.save(searchResult);
		
		if(generatedId != null) {
			ok = new SearchResultAttributesDao().saveSearchingAttributes(objectAttributes, searchResult);
			if(!ok.equals("OK")) {
				session.getTransaction().rollback();
				info = "Błąd w trakcie tworzenia obiektu SearchResultAttributes: " + ok;
			}
		}
		else
			info = "Błąd w trakcie tworzenia obiektu SearchResult";
		
		session.getTransaction().commit();
		session.close();
		return info;
	}
	
	@SuppressWarnings("unchecked")
	public List<SearchResultDTO> getSearchResultForObject(long objectId) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		
		Objects objects = new ObjectsDao().getObject(objectId);
		
		List<SearchResult> searchResult = new ArrayList<SearchResult>(session.createCriteria(SearchResult.class).add(Restrictions.eq("objects", objects)).list());
		List<SearchResultDTO> searchResultDTO = new ArrayList<SearchResultDTO>(searchResult != null ? searchResult.size() : 0);
		for(SearchResult tmpSearchResult : searchResult)
			searchResultDTO.add(createSearchResultDTO(tmpSearchResult, false));
		
		session.getTransaction().commit();
		session.close();
		return searchResultDTO;
	}
	
	@SuppressWarnings("unchecked")
	public List<SearchResultDTO> getAllSearchResults() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<SearchResult> searchResult = new ArrayList<SearchResult>(session.createQuery("from SearchResult").list());
		List<SearchResultDTO> searchResultDTO = new ArrayList<SearchResultDTO>(searchResult != null ? searchResult.size() : 0);
		for(SearchResult tmpSearchResult : searchResult)
			searchResultDTO.add(createSearchResultDTO(tmpSearchResult, true));
		
		session.getTransaction().commit();
		session.close();
		return searchResultDTO;
	}
	
	public SearchResultDTO createSearchResultDTO(SearchResult searchResult, boolean loadValues) {
		SearchResultDTO searchResultDTO = new SearchResultDTO();
		searchResultDTO.setSearchResultId(searchResult.getSearchResultId());
		searchResultDTO.setDate(searchResult.getDate());
		searchResultDTO.setObjects(new ObjectsDao().createObjectsDTO(searchResult.getObjects()));
		
		if(loadValues) {
			Set<SearchResultAttributesDTO> searchResultAttributeDTO = new HashSet<SearchResultAttributesDTO>();
			for(SearchResultAttributes searchResultAttributes : searchResult.getObjectAttributes())
				searchResultAttributeDTO.add(new SearchResultAttributesDTO(searchResultAttributes.getSearchResultAttributesId(),
																		   null,
																		   searchResultAttributes.getAttributeName(),
																		   searchResultAttributes.getAttributeValue()));
			
			searchResultDTO.setObjectAttributes(searchResultAttributeDTO);
		}
		return searchResultDTO;
	}
}
